package com.bjsxt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.constants.Constants;
import com.bjsxt.domain.*;
import com.bjsxt.dto.PurchaseDto;
import com.bjsxt.dto.PurchaseFormDto;
import com.bjsxt.dto.PurchaseItemDto;
import com.bjsxt.mapper.InventoryLogMapper;
import com.bjsxt.mapper.MedicinesMapper;
import com.bjsxt.mapper.PurchaseItemMapper;
import com.bjsxt.mapper.PurchaseMapper;
import com.bjsxt.service.PurchaseService;
import com.bjsxt.utils.IdGeneratorSnowflake;
import com.bjsxt.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * 采购入库审核service接口实现类
 */
@Service
@Transactional
public class PurchaseServiceImpl implements PurchaseService {

    @Autowired
    private PurchaseMapper purchaseMapper;

    @Autowired
    private PurchaseItemMapper purchaseItemMapper;

    @Autowired
    private InventoryLogMapper inventoryLogMapper;

    @Autowired
    private MedicinesMapper medicinesMapper;

    /**
     * 分页查询入库单
     *
     * @param purchaseDto
     * @return
     */
    @Override
    public DataGridView listPurchaseForPage(PurchaseDto purchaseDto) {
        Page<Purchase> page = new Page<>(purchaseDto.getPageNum(), purchaseDto.getPageSize());
        QueryWrapper<Purchase> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(purchaseDto.getProviderId()), Purchase.COL_PROVIDER_ID, purchaseDto.getProviderId());
        queryWrapper.eq(StringUtils.isNotBlank(purchaseDto.getStatus()), Purchase.COL_STATUS, purchaseDto.getStatus());
        queryWrapper.like(StringUtils.isNotBlank(purchaseDto.getApplyUserName()), Purchase.COL_APPLY_USER_NAME, purchaseDto.getApplyUserName());
        queryWrapper.orderByDesc(Purchase.COL_CREATE_TIME);
        this.purchaseMapper.selectPage(page, queryWrapper);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    /**
     * 根据采购单据号查询单据信息
     *
     * @param purchaseId
     * @return
     */
    @Override
    public Purchase getPurchaseById(String purchaseId) {
        return this.purchaseMapper.selectById(purchaseId);
    }

    /**
     * 提交审核
     *
     * @param purchaseId
     * @param simpleUser
     * @return
     */
    @Override
    public int doAudit(String purchaseId, SimpleUser simpleUser) {
        Purchase purchase = this.purchaseMapper.selectById(purchaseId);
        purchase.setStatus(Constants.STOCK_PURCHASE_STATUS_2);  //设置状态为待审核
        purchase.setApplyUserName(simpleUser.getUserName());
        purchase.setApplyUserId(Long.valueOf(simpleUser.getUserId().toString()));
        return this.purchaseMapper.updateById(purchase);
    }

    /**
     * 作废
     *
     * @param purchaseId
     * @return
     */
    @Override
    public int doInvalid(String purchaseId) {
        Purchase purchase = this.purchaseMapper.selectById(purchaseId);
        purchase.setStatus(Constants.STOCK_PURCHASE_STATUS_5);  //设置状态为作废
        return this.purchaseMapper.updateById(purchase);
    }

    /**
     * 审核通过
     *
     * @param purchaseId
     * @return
     */
    @Override
    public int auditPass(String purchaseId) {
        Purchase purchase = this.purchaseMapper.selectById(purchaseId);
        purchase.setStatus(Constants.STOCK_PURCHASE_STATUS_3);  //设置状态为审核通过
        return this.purchaseMapper.updateById(purchase);
    }

    /**
     * 审核不通过
     *
     * @param purchaseId
     * @param auditMsg
     * @return
     */
    @Override
    public int auditNoPass(String purchaseId, String auditMsg) {
        Purchase purchase = this.purchaseMapper.selectById(purchaseId);
        purchase.setStatus(Constants.STOCK_PURCHASE_STATUS_4);  //设置状态为审核不通过
        purchase.setExamine(auditMsg);
        return this.purchaseMapper.updateById(purchase);
    }

    /**
     * 根据id查询一个采购信息详情
     *
     * @param purchaseId
     * @return
     */
    @Override
    public List<PurchaseItem> getPurchaseItemById(String purchaseId) {
        if (null != purchaseId) {
            QueryWrapper<PurchaseItem> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(PurchaseItem.COL_PURCHASE_ID, purchaseId);
            return this.purchaseItemMapper.selectList(queryWrapper);
        }
        return Collections.EMPTY_LIST;  //返回空集合对象【静态常量】节省内存资源
    }

    /**
     * 暂存采购单数据和详情【暂存--采购单的状态是Constants.STOCK_PURCHASE_STATUS_1】
     *
     * @param purchaseFormDto
     * @return
     */
    @Override
    public int addPurchaseAndItem(PurchaseFormDto purchaseFormDto) {
        Purchase purchase = this.purchaseMapper.selectById(purchaseFormDto.getPurchaseDto().getPurchaseId());
        if (null != purchase) {
//            删除之前的数据
            this.purchaseMapper.deleteById(purchase.getPurchaseId());
//            删除之前的详细数据
            QueryWrapper<PurchaseItem> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(PurchaseItem.COL_PURCHASE_ID, purchase.getPurchaseId());
            this.purchaseItemMapper.delete(queryWrapper);
        }
//        保存采购单主表数据
        Purchase newPurchase = new Purchase();
        BeanUtil.copyProperties(purchaseFormDto.getPurchaseDto(), newPurchase);
        newPurchase.setStatus(Constants.STOCK_PURCHASE_STATUS_1);//未提交状态
        newPurchase.setCreateBy(purchaseFormDto.getPurchaseDto().getSimpleUser().getUserName());
        newPurchase.setCreateTime(DateUtil.date());
        int i = this.purchaseMapper.insert(newPurchase);
//        保存详情数据
        for (PurchaseItemDto itemDto : purchaseFormDto.getPurchaseItemDtos()) {
            PurchaseItem purchaseItem = new PurchaseItem();
            BeanUtil.copyProperties(itemDto, purchaseItem);
            purchaseItem.setPurchaseId(newPurchase.getPurchaseId());
            purchaseItem.setItemId(IdGeneratorSnowflake.snowflakeId().toString());
            this.purchaseItemMapper.insert(purchaseItem);
        }
        return i;
    }

    /**
     * 保存并提交审核采购单详情
     *
     * @param purchaseFormDto
     * @return
     */
    @Override
    public int addPurchaseAndItemToAudit(PurchaseFormDto purchaseFormDto) {
        Purchase purchase = this.purchaseMapper.selectById(purchaseFormDto);
        if (null != purchase) {
//            删除之前的数据
            this.purchaseMapper.deleteById(purchase.getPurchaseId());
//            删除之前的详细数据
            QueryWrapper<PurchaseItem> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(PurchaseItem.COL_PURCHASE_ID, purchase.getPurchaseId());
            this.purchaseItemMapper.delete(queryWrapper);
        }
//        保存采购单主表数据
        Purchase newPurchase = new Purchase();
        BeanUtil.copyProperties(purchaseFormDto.getPurchaseDto(), newPurchase);
        newPurchase.setStatus(Constants.STOCK_PURCHASE_STATUS_2);  //设置为待审核
        newPurchase.setCreateBy(purchaseFormDto.getPurchaseDto().getSimpleUser().getUserName());
        newPurchase.setCreateTime(DateUtil.date());
//        设置申请人和申请人的id
        newPurchase.setApplyUserName(purchaseFormDto.getPurchaseDto().getSimpleUser().getUserName());
        newPurchase.setApplyUserId(Long.valueOf(purchaseFormDto.getPurchaseDto().getSimpleUser().getUserId().toString()));
        int i = this.purchaseMapper.insert(newPurchase);
//        保存详情数据
        for (PurchaseItemDto item : purchaseFormDto.getPurchaseItemDtos()) {
            PurchaseItem purchaseItem = new PurchaseItem();
            BeanUtil.copyProperties(item, purchaseItem);
            purchaseItem.setPurchaseId(newPurchase.getPurchaseId());
            purchaseItem.setItemId(IdGeneratorSnowflake.snowflakeId().toString());
            this.purchaseItemMapper.insert(purchaseItem);
        }
        return i;
    }

    /**
     * 入库逻辑【向stock_inventory_log表里面添加数据 并更新stock_medicines的库存】
     *
     * @param purchaseId
     * @param currentSimpleUser
     * @return
     */
    @Override
    public int doInventory(String purchaseId, SimpleUser currentSimpleUser) {
        Purchase purchase = this.purchaseMapper.selectById(purchaseId);
        if (null != purchase || purchase.getStatus().equals(Constants.STOCK_PURCHASE_STATUS_3)) {
//            查询详情
            QueryWrapper<PurchaseItem> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(PurchaseItem.COL_PURCHASE_ID, purchase.getPurchaseId());
            List<PurchaseItem> purchaseItems = this.purchaseItemMapper.selectList(queryWrapper);
            for (PurchaseItem purchaseItem : purchaseItems) {
                InventoryLog inventoryLog = new InventoryLog();
                inventoryLog.setInventoryLogId(purchaseItem.getItemId());
                inventoryLog.setPurchaseId(purchaseItem.getPurchaseId());
                inventoryLog.setMedicinesId(purchaseItem.getMedicinesId());
                inventoryLog.setInventoryLogNum(purchaseItem.getPurchaseNumber());
                inventoryLog.setTradePrice(purchaseItem.getTradePrice());
                inventoryLog.setTradeTotalAmount(purchaseItem.getTradeTotalAmount());
                inventoryLog.setBatchNumber(purchaseItem.getBatchNumber());
                inventoryLog.setMedicinesName(purchaseItem.getMedicinesName());
                inventoryLog.setMedicinesType(purchaseItem.getMedicinesType());
                inventoryLog.setPrescriptionType(purchaseItem.getPrescriptionType());
                inventoryLog.setProducterId(purchaseItem.getProducterId());
                inventoryLog.setConversion(purchaseItem.getConversion());
                inventoryLog.setUnit(purchaseItem.getUnit());
                inventoryLog.setCreateTime(DateUtil.date());
                inventoryLog.setCreateBy(currentSimpleUser.getUserName());
                this.inventoryLogMapper.insert(inventoryLog);
//                更新药品库存
                Medicines medicines = this.medicinesMapper.selectById(purchaseItem.getMedicinesId());
                medicines.setMedicinesStockNum(medicines.getMedicinesStockNum() + purchaseItem.getPurchaseNumber());
                medicines.setUpdateBy(currentSimpleUser.getUserName());
                this.medicinesMapper.updateById(medicines);
            }
//            入库成功【修改单据的状态为入库成功】
            purchase.setStatus(Constants.STOCK_PURCHASE_STATUS_6);
            purchase.setStorageOptTime(DateUtil.date());
            purchase.setStorageOptUser(currentSimpleUser.getUserName());
            return this.purchaseMapper.updateById(purchase);
        }
        return -1;
    }
}
